package com.numberone.web.controller.xycard;

import java.math.BigDecimal;
import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.numberone.common.annotation.Log;
import com.numberone.common.enums.BusinessType;
import com.numberone.system.domain.XyCard;
import com.numberone.system.service.IXyCardService;
import com.numberone.framework.web.base.BaseController;
import com.numberone.common.page.TableDataInfo;
import com.numberone.common.base.AjaxResult;
import com.numberone.common.utils.poi.ExcelUtil;

/**
 * 欣亿卡 信息操作处理
 * 
 * @author numberone
 * @date 2020-10-10
 */
@Controller
@RequestMapping("/system/xyCard")
public class XyCardController extends BaseController
{
    private String prefix = "system/xyCard";
	
	@Autowired
	private IXyCardService xyCardService;
	
	@RequiresPermissions("system:xyCard:view")
	@GetMapping()
	public String xyCard()
	{
	    return prefix + "/xyCard";
	}
	
	/**
	 * 查询欣亿卡列表
	 */
	@RequiresPermissions("system:xyCard:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(XyCard xyCard)
	{
		startPage();
        List<XyCard> list = xyCardService.selectXyCardList(xyCard);
		return getDataTable(list);
	}
	
	
	/**
	 * 导出欣亿卡列表
	 */
	@RequiresPermissions("system:xyCard:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(XyCard xyCard)
    {
    	List<XyCard> list = xyCardService.selectXyCardList(xyCard);
        ExcelUtil<XyCard> util = new ExcelUtil<XyCard>(XyCard.class);
        return util.exportExcel(list, "xyCard");
    }
	
	/**
	 * 新增欣亿卡
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存欣亿卡
	 */
//	@RequiresPermissions("system:xyCard:add")
//	@Log(title = "欣亿卡", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(XyCard xyCard)
	{
		Boolean flag = true;
		while(flag)
		{
			flag = true;
			while(flag)
			{
				String cardNum = new CardCodeFactory().toCode();
				int num = xyCardService.isCardNum(cardNum);
				if (num <= 0)
				{
					xyCard.setCardNum(cardNum);
					flag = false;
				}
			}
			flag = true;
			while (flag)
			{
				String cardPwd = new CardCodeFactory().toPwd();
				int pwd = xyCardService.isCardPwd(cardPwd);
				if (pwd <= 0)
				{
					xyCard.setCardPwd(cardPwd);
					flag = false;
				}
			}
		}
		xyCard.setPrice(new BigDecimal(10000));
		xyCard.setStatus(0);
		return toAjax(xyCardService.insertXyCard(xyCard));
	}

	/**
	 * 修改欣亿卡
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Long id, ModelMap mmap)
	{
		XyCard xyCard = xyCardService.selectXyCardById(id);
		mmap.put("xyCard", xyCard);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存欣亿卡
	 */
	@RequiresPermissions("system:xyCard:edit")
	@Log(title = "欣亿卡", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(XyCard xyCard)
	{		
		return toAjax(xyCardService.updateXyCard(xyCard));
	}
	
	/**
	 * 删除欣亿卡
	 */
	@RequiresPermissions("system:xyCard:remove")
	@Log(title = "欣亿卡", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(xyCardService.deleteXyCardByIds(ids));
	}
	
}
